import { Body, Controller, Get, Post, Res, HttpStatus, UploadedFile, UseInterceptors, HttpException, Query, Request } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { Response } from 'express';
import { InternetCelebrity } from './internetCelebrity.entity';
import { InternetCelebrityService } from './internetCelebrity.service';
import {
  get
} from 'request'

import {join, resolve} from 'path'
import { createWriteStream } from 'fs';
import {
  wx_appid,
  wx_secret,
  wx_api
} from '../../setting'
import {
  existsSync,
  mkdirSync
} from 'fs'

import {
  apibefore
} from '../../router'
import { UserService } from '../user/user.service';
import { User } from '../user/user.entity';
import { ValidUniqueLoginInterceptor } from 'src/interceptor/validUniqueLogin';
@Controller(apibefore('/internetCelebrity_background'))
export class InternetCelebrityControllerBackground {
  constructor(private readonly internetCelebrityService: InternetCelebrityService,
    private readonly userService: UserService
    ) {}

  
  @Get('/findAll_ByAccount_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async findAll_ByAccount_auth(@Query() params, @Request() req){
    return await this.internetCelebrityService.findAll_ByAccount(params.account)
  }

  @Get('/findAll_ByAccount')
  async findAll_ByAccount(@Query() params, @Request() req){
    return await this.internetCelebrityService.findAll_ByAccount(params.account)
  }

  
  @Get('/findAll_ByName_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async findAll_ByName_auth(@Query() params) {
    return await this.internetCelebrityService.findAll_ByName(params.name)
  }

  @Get('/findAll_ByName')
  async findAll_ByName(@Query() params) {
    return await this.internetCelebrityService.findAll_ByName(params.name)
  }

  @Get('/findAll_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async findAll_auth(@Query() params, @Request() req) {
    let token: number = req.headers.token
    //查询用户的wx_user
    let findUser:User = await this.userService.findOne(token)
    if(!findUser){
      return new HttpException('用户不存在',HttpStatus.INTERNAL_SERVER_ERROR)
    }
    let pageSize: number = parseInt(params.pageSize)
    let pageIndex: number = parseInt(params.pageIndex)
    let skip: number = pageSize * pageIndex
    return await this.internetCelebrityService.findAll(skip, pageSize, params.keyword, params.platformList || [],findUser.wx_user)
  }


  @Get('/findAll')
  async findAll(@Query() params, @Request() req) {
    let token: number = req.headers.token
    //查询用户的wx_user
    let findUser:User = await this.userService.findOne(token)
    if(!findUser){
      return new HttpException('用户不存在',HttpStatus.INTERNAL_SERVER_ERROR)
    }
    let pageSize: number = parseInt(params.pageSize)
    let pageIndex: number = parseInt(params.pageIndex)
    let skip: number = pageSize * pageIndex
    return await this.internetCelebrityService.findAll(skip, pageSize, params.keyword, params.platformList || [],findUser.wx_user)
  }

  @Get('/findAll_ByPlatform_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async findAll_ByPlatform_auth(@Query() params, @Request() req) {
    let token: number = req.headers.token
    //查询用户的wx_user
    let findUser:User = await this.userService.findOne(token)
    if(!findUser){
      return new HttpException('用户不存在',HttpStatus.INTERNAL_SERVER_ERROR)
    }
    return await this.internetCelebrityService.findAll_ByPlatform(params.platform,findUser.wx_user)
  }

  @Get('/findAll_ByPlatform')
  async findAll_ByPlatform(@Query() params, @Request() req) {
    let token: number = req.headers.token
    //查询用户的wx_user
    let findUser:User = await this.userService.findOne(token)
    if(!findUser){
      return new HttpException('用户不存在',HttpStatus.INTERNAL_SERVER_ERROR)
    }
    return await this.internetCelebrityService.findAll_ByPlatform(params.platform,findUser.wx_user)
  }


  @Post('update_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async update_auth(@Body() data: InternetCelebrity){
    let updateResult = await this.internetCelebrityService.update(data.name,data.platform,data.id)
    if(updateResult.affected > 0){
      return true
    }else{
      return new HttpException('修改失败', HttpStatus.INTERNAL_SERVER_ERROR)
    }
  }

  @Post('update')
  async update(@Body() data: InternetCelebrity){
    let updateResult = await this.internetCelebrityService.update(data.name,data.platform,data.id)
    if(updateResult.affected > 0){
      return true
    }else{
      return new HttpException('修改失败', HttpStatus.INTERNAL_SERVER_ERROR)
    }
  }

  
  @Post('create_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async create_auth(@Body() data: InternetCelebrity, @Request() req) {
    let token: number = req.headers.token
    //查询用户的wx_user
    let findUser:User = await this.userService.findOne(token)
    if(!findUser){
      return new HttpException('用户不存在',HttpStatus.INTERNAL_SERVER_ERROR)
    }
    data.wx_user = findUser.wx_user
    data.create_time = new Date()
    return await this.internetCelebrityService.create(data)
  }


  @Post('create')
  async create(@Body() data: InternetCelebrity, @Request() req) {
    let token: number = req.headers.token
    //查询用户的wx_user
    let findUser:User = await this.userService.findOne(token)
    if(!findUser){
      return new HttpException('用户不存在',HttpStatus.INTERNAL_SERVER_ERROR)
    }
    data.wx_user = findUser.wx_user
    data.create_time = new Date()
    return await this.internetCelebrityService.create(data)
  }

  @Post('delete_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async delete_auth(@Body() data: InternetCelebrity){
    return await this.internetCelebrityService.remove(data.id)
  }

  @Post('delete')
  async delete(@Body() data: InternetCelebrity){
    return await this.internetCelebrityService.remove(data.id)
  }

}
